package com.example.coursedesign.ui.home;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.coursedesign.R;

public class VideoDetail extends AppCompatActivity {
    private VideoView videoView;
    private TextView descriptionTextView;
    private Button playButton, pauseButton;
    private ImageView thumbnailImageView;
    private boolean isPlaying = false; // 新增变量以跟踪视频播放状态

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_detail); // 确保这是正确的布局文件

        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 获取传入的数据
        String videoPath = getIntent().getStringExtra("videoPath");
        String description = getIntent().getStringExtra("description");
        int thumbnailResourceId = getIntent().getIntExtra("thumbnailId", 0); // 使用 getIntExtra 接收整数值

        // 找到对应的视图并设置数据
        videoView = findViewById(R.id.videoView);
        descriptionTextView = findViewById(R.id.videoDescription); // 使用与布局文件中一致的ID
        playButton = findViewById(R.id.playButton);
        pauseButton = findViewById(R.id.pauseButton);
        thumbnailImageView = findViewById(R.id.thumbnailImageView);

        if (videoPath != null) {
            Uri videoUri = Uri.parse(videoPath);
            videoView.setVideoURI(videoUri);
            videoView.setOnPreparedListener(mp -> {
                mp.setLooping(true); // 循环播放
            });
        } else {
            Toast.makeText(this, "Invalid video path.", Toast.LENGTH_SHORT).show();
        }

        if (description != null) {
            descriptionTextView.setText(description);
        } else {
            descriptionTextView.setText("No description available.");
        }

        // 按照要求设置缩略图
        if (thumbnailResourceId != 0) {
            thumbnailImageView.setImageResource(thumbnailResourceId);
            thumbnailImageView.setVisibility(View.VISIBLE); // 确保缩略图默认可见
        } else {
            thumbnailImageView.setVisibility(View.GONE);
        }

        // 设置播放按钮点击事件
        playButton.setOnClickListener(v -> {
            if (!isPlaying) {
                videoView.start();
                isPlaying = true;
                thumbnailImageView.setVisibility(View.GONE); // 隐藏缩略图
                updateButtonStates();
            }
        });

        // 设置暂停按钮点击事件
        pauseButton.setOnClickListener(v -> {
            if (isPlaying && videoView.isPlaying()) {
                videoView.pause();
                isPlaying = false;
//                thumbnailImageView.setVisibility(View.VISIBLE); // 显示缩略图
                updateButtonStates();
            }
        });

        // 初始化按钮状态和缩略图显示状态
        updateButtonStates();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed(); // 返回上一个 Activity
        return true;
    }

    private void updateButtonStates() {
        // 根据视频播放状态更新按钮状态
        playButton.setEnabled(!isPlaying || !videoView.isPlaying());
        pauseButton.setEnabled(isPlaying && videoView.isPlaying());
//        thumbnailImageView.setVisibility(isPlaying && videoView.isPlaying() ? View.GONE : View.VISIBLE);
    }
}